package top.leyi.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.leyi.IService.ICollectionService;
import top.leyi.entity.SQLUtil;
import top.leyi.entity.Talk.SimpleTalk;
import top.leyi.entity.pushAndColle.Collection0;
import top.leyi.entity.user.User;
import top.leyi.mapper.CollectionMapper;
import top.leyi.mapper.TalkMapper;

import java.util.ArrayList;
import java.util.List;
@Service
public class CollectionServiceImpl implements ICollectionService {

    private  final  int LIMIT = 10;
    @Autowired
    CollectionMapper collectionMapper;
    @Autowired
    TalkMapper talkMapper;

    @Override
    public List<Collection0> getAllCollection(User user, int page) {

        SQLUtil sqlUtil = new SQLUtil();
        sqlUtil.setId(user.getId());
        sqlUtil.setLimit(LIMIT);
        sqlUtil.setOffset((page - 1) * LIMIT);

        List<Collection0> collections = collectionMapper.getOwnColl(sqlUtil);

        List<SimpleTalk> talks =  new ArrayList<>();
        collections.forEach((e)->{
            e.setContent(talkMapper.querySimpleTalk(e.getOriginal()));
        });
        return collections ;
    }

    @Override
    public void addOneCollection(Collection0 collection0, User user) {
        ///如果这条说说未被这个人收藏过，那么进行收藏
         if(collectionMapper.queryHasCol(collection0.getOriginal(),user.getId()) == null) {
             collectionMapper.addColl(collection0);
         }

    }

    @Override
    public void removeCollection(Collection0 collection0, User user) {
         collectionMapper.removeColl(collection0);
    }

}
